<?php

class Model
{
	private $model;
    public $select_db = DEFAULT_DB_NAME;
    public $select_redis = DEFAULT_REDIS_NAME;

    static public function getInstance($model)
    {
        $model = ucwords($model);
        static $instances = array();

        if (! isset($instances[$model]))
        {
            $file = strtolower($model);
            if (file_exists(MODEL_PATH . $file . '.php')){
                include_once MODEL_PATH . $file . '.php';
			    $classname = $model.'Model';
                $instances[$model] = new $classname();
                $instances[$model]->model = $model;
            }else{
                p('没有找到Model文件：'.MODEL_PATH.$file.'.php');
            }
        }
        return $instances[$model];
    }
    
    final public function db($db = '')
    {
        $db = $db ? $db : $this->select_db;
        return DBFactory::getInstance($db);
    }

    final public function redis($redis = '')
    {
        $redis = $redis ? $redis : $this->use_redis;
        return RedisFactory::getInstance($redis);
    }

//    final public function pager($curr_page = 1, $per_page = 10, $db = '')
//    {
//        $db = $db ? $db : $this->select_db;
//        include_once LIB_PATH . 'pager.php';
//        return new SnowPager($this->db($db), $curr_page, $per_page);
//    }

    final public function pager($curr_page = 1, $per_page = 10, $ct_db_name = '', $select_db_name = '')
    {
        include_once LIB_PATH . 'pager.php';
        $ct_db_name = $ct_db_name ? $ct_db_name : $this->use_db;
        if (! $select_db_name)
        {
            $select_db_name = $ct_db_name;
        }
        return new SnowPager(array('ct'=>$this->db($ct_db_name), 'select'=>$this->db($select_db_name)), $curr_page, $per_page);
    }

    final public function input(){
        return Input::getInstance();
    }
    
    final public function session(){
        return Session::getInstance();
    }

    final public function cookie(){
        return Cookie::getInstance();
    }
}